package com.example.clientsideapplication;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.Map.Entry;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class ViewCachingOfferActivity extends Activity {

	private String cachingOfferFileName = "cachingOfferFile";
	private ArrayList<Entry<Merchant, Offer>> offerArrayList;
	private ListView offerViewList;
	private Context context;
	private FileInputStream fileInputStream;
	private ObjectInputStream objectInputStream;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_offer);
		
		context = getApplicationContext();
		
		try {
			fileInputStream = context.openFileInput(cachingOfferFileName);
			objectInputStream = new ObjectInputStream(fileInputStream);
			offerArrayList = (ArrayList<Entry<Merchant, Offer>>) objectInputStream.readObject();
			objectInputStream.close();
				
			offerViewList = (ListView) findViewById(R.id.offer_list);
			offerViewList.setAdapter(new OfferClientListAdapter(this,
					R.layout.row_list_layout, offerArrayList));
			registerForContextMenu(offerViewList);
			offerViewList
					.setOnItemClickListener(new AdapterView.OnItemClickListener() {
						public void onItemClick(AdapterView parent, View v, int position, long id) {
							Entry<Merchant, Offer> item = offerArrayList.get(position);
							Intent intent = new Intent(
									ViewCachingOfferActivity.this,
									ViewClientOfferInfo.class);
							intent.putExtra("merchant", item.getKey());
							intent.putExtra("offer", item.getValue());
							startActivity(intent);
						}
					});
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (StreamCorruptedException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}		
}

